
import os
import http.client
import asyncio

import json
from dotenv import load_dotenv
from createbox import createbox
from add_part import add_part
load_dotenv()
from take_prtsc import take_screenshot
from urllib.parse import urlparse
from open_directory import open_sw_part_folder
from assemblyhole import create_concentric_mate
# 配置日志
'''

image_promt={
        "role": "user",
        "content": [
            {"type": "text", 
            "text": "帮我............."},
            {"type": "image_url", 
                            "image_url": {
                "url": f"data:image/jpeg;base64,{base64_image}"
            }},
        ]
    }

'''
tools = [{
    "type": "function",
    "function": {
        "name": "execute",
        "description": "use freecad's function",
        "parameters": {
        "type": "object",
        "properties": {
            "function": {
                "type": "string",
                "description": """
*返回单条纯函数代码,不要有注释*
**************************单条纯函数代码*********************************     

函数代码:createbox()
描述:此方法可选输入长宽高信息
例子:createbox(10,10,50)

函数代码:create_concentric_mate()
描述:在选好同心后运行此方法可以完成同心配合
例子:create_concentric_mate()

函数代码:add_part()
描述:此方法不可选输入零件名称
例子：如果要生成一体式油水分离器输出：
add_part([
"电机",
"主体1"

])而不是add_part(["一体式油水分离器"]) 


                """},
        
        },
    
    }}}
]
        

        
             
# llm.py (追加到文件末尾或创建新文件)

from flask import Flask, request, jsonify
import traceback

app = Flask(__name__)
from flask_cors import CORS
CORS(app)
@app.route('/execute', methods=['POST'])
def execute_command():
    """
    接收 POST 请求，执行其中包含的 Python 命令字符串。
    请求体应为 JSON 格式，包含一个 'command' 键。
    例如: {"command": "create_concentric_mate()"}
    """
    try:
        data = request.get_json()
        if not data or 'command' not in data:
            return jsonify({"error": "Missing 'command' in request body"}), 400
        
        command = data['command']
        # 执行传入的命令
        exec(command)
        return jsonify({"status": "success", "executed": command}), 200
    except Exception as e:
        # 捕获并返回执行错误
        error_info = traceback.format_exc()
        return jsonify({"error": str(e), "traceback": error_info}), 500

if __name__ == '__main__':
    # 启动 Flask 应用，监听 5004 端口
    app.run(host='0.0.0.0', port=5004, debug=False)
 
      
